package cn.xiaohaoo.datacleaning;

import cn.xiaohaoo.medicine.util.mongo.MongoDatabaseUtils;
import org.bson.Document;
import org.bson.types.ObjectId;

/**
 * 增添图片等数据
 */
public class DataFix {
    public static void main(String[] args) {
        MongoDatabaseUtils.queryAllDocuments(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION)
            .forEach(v -> {
                Document document = new Document();
                Document queryDocument = new Document().append("name",
                    v.get("name").toString());
                Document queryDocumentByName = MongoDatabaseUtils
                    .queryDocumentByName(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION, queryDocument);
                if (queryDocumentByName != null) {
                    Object img2 = queryDocumentByName.get("img");
                    if (img2 != null) {
                        document.put("$set", new Document("img2", img2.toString()));
                    }
                }


                Document queryDocumentByName2 = MongoDatabaseUtils.queryDocumentByName(
                    MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION, queryDocumentByName);
                if (queryDocumentByName2 != null) {
                    Object img3 = queryDocumentByName2.get("img");
                    if (img3 != null) {
                        document.put("$set", new Document("img3", img3.toString()));
                    }
                }

                document.append("$rename", new Document("full_name", "中药名称"));
                document.append("$unset", new Document("title", ""));

                if (document.size() != 0) {
                    try {
                        MongoDatabaseUtils.updateDocument(MongoDatabaseUtils.MEDICINE_DETAIL_COLLECTION,
                            new Document("_id", new ObjectId(v.get("_id").toString())),
                            document);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
    }
}
